package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import controller.controllerFunc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

public class LoginWnd extends JFrame{
	private		JSplitPane		splitPaneV;
	private		JPanel			panelFields;
	private		JPanel		 	panelActions;
	private   	JTextField      txtUserName;
	private 	JPasswordField  txtPassword;

	
	//TODO: Move it to main
	private HelpWnd   helpWin;

	public LoginWnd()
	{
		setTitle( "FW Login" );
		JPanel topPanel = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		getContentPane().add( topPanel );

		// Create the panels
		createPanelFields();
		createPaneActions();

		// Create a splitter pane
		splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
		topPanel.add( splitPaneV, BorderLayout.CENTER );
		splitPaneV.setLeftComponent( panelFields);
		splitPaneV.setRightComponent(panelActions);
	
		pack();
		setSize(new Dimension(300, 150));
		setLocationRelativeTo(null);
		setEnabled(true);
		setVisible(true);

	}

	private void createPanelFields()
	{
		txtUserName = new JTextField();
		txtPassword = new JPasswordField();
		JLabel lblUserName = new JLabel("User Name: ");
		JLabel lblPassword = new JLabel("Paswword: " );

		panelFields = new JPanel();
		panelFields.setLayout(new BoxLayout(panelFields, BoxLayout.X_AXIS));
		panelFields.add(lblUserName);
		panelFields.add(txtUserName);
		panelFields.setLayout(new BoxLayout(panelFields, BoxLayout.Y_AXIS));
		panelFields.add(lblPassword);
		panelFields.add(txtPassword);
	}
	
	private void createPaneActions()
	{
		JButton loginButton = new JButton( "Login" );
		JButton helpButton = new JButton( "Help" );

		panelActions = new JPanel();
		panelActions.setLayout(new BoxLayout(panelActions, BoxLayout.X_AXIS));
		panelActions.add(loginButton);
		panelActions.add(helpButton);
		
		loginButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ae) 
		    {
				LoginFW();
			}
		}
		);	
		
		helpButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent ae) 
		    {
				ShowHelp("Login Window");
			}
		}
		);	

	}

	public void LoginFW()
	{
		// case no data insert into user or password fields
		if((txtUserName.getText().equals("") || txtPassword.getText().equals("")))
		{
			JOptionPane.showMessageDialog(this,
			"You must enter User Name & Pasword before Login");
			return;
		}			
		else
		{
			boolean isAuthorize = controllerFunc.PerformLogin(txtUserName.getText(), txtPassword.getText());
			
			if(isAuthorize)
			{
				JOptionPane.showMessageDialog(this, "Maya  = To Open the main window");
			}	
			else 
			{
				JOptionPane.showMessageDialog(this, "You are not authorized to perform login");
				JOptionPane.showMessageDialog(this, "Maya  = To write to the error file");
			}
		}
	}	
	
	public void ShowHelp(String hlpTopic)
	{
		if(helpWin == null)
		{
			helpWin = new HelpWnd();
			helpWin.ShowDesc(hlpTopic);
		}					
	}
}
